<?php
class TICE_Controlador_CrudArquivos extends TICE_Controlador_ListaArquivos {
	
	/**
	 * Opções de upload
	 * @var array(campo => opcoes)
	 */
	public $uploads;
	
	function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array() ) {
		parent::__construct($request, $response, $invokeArgs);
		if (empty($this->caminho)) {
			$this->caminho = BASE_PATH . '/arquivos';
		}
		if (empty($this->validacoes)) {
			$this->validacoes = array('IsImage');
		}
	}
	protected function erroToString($error_code) {
		switch ($error_code) { 
			case UPLOAD_ERR_OK: 
				return 'There is no error, the file uploaded with success.'; 
			case UPLOAD_ERR_INI_SIZE: 
				return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; 
			case UPLOAD_ERR_FORM_SIZE: 
				return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; 
			case UPLOAD_ERR_PARTIAL: 
				return 'The uploaded file was only partially uploaded'; 
			case UPLOAD_ERR_NO_FILE: 
				return 'No file was uploaded'; 
			case UPLOAD_ERR_NO_TMP_DIR: 
				return 'Missing a temporary folder'; 
			case UPLOAD_ERR_CANT_WRITE: 
				return 'Failed to write file to disk'; 
			case UPLOAD_ERR_EXTENSION: 
				return 'File upload stopped by extension'; 
			default: 
				return 'Unknown upload error';
		} 
	}
	/**
	 * Mostra o formulário para inserção
	 * @param int $_GET['id']
	 */
	public function formAction() {
		$this->view->action = 'insere';
	}
	/**
	 * Insere um registro
	 * @param array $campos Os campos de trataForm()
	 * @return
	 * 		int $this->view->id,
	 * 		bool $this->view->sucesso,
	 * 		string $this->view->erro
	 */
	public function insereAction() {
		try {
				// trata o novo arquivo
//echo 'f'; var_dump($_FILES);
//var_dump($this->caminho);
			if (!empty($_FILES)) {
				$adapter = new Zend_File_Transfer_Adapter_Http();
				$adapter->setValidators($this->validacoes);
				$adapter->setDestination($this->caminho);
//				if (!$adapter->isUploaded()) {
////var_dump(2);
//					throw new Exception('Não conseguiu fazer upload');
//				}
//				if (!$adapter->isValid()) {
////var_dump(1);
//					throw new Exception('Arquivo inválido');
//				}
				if (!$adapter->receive()) {
					throw new Exception('Algum erro na hora de mover: ' . $this->erroToString($_FILES[$campo]['error']) . ' | ' . json_encode($adapter->getMessages()));
				}
			}

			$this->view->arquivos = $adapter->getFileInfo();
			$sucesso = true;
		}
		catch (Exception $e) {
			$sucesso = false;
			$this->view->erro = $e;
		}
		$this->view->sucesso = $sucesso;
	}
	
	/**
	 * Apaga um registro
	 * @param int $_GET['id']
	 * @return int $this->view->total O número de linhas apagadas
	 * @return bool $this->view->sucesso
	 * @return string $this->view->erro
	 */
	public function apagaAction() {
		$id = $_GET['a'];
		
		try {
			Zend_Search_Lucene_Storage_Directory_Filesystem::deleteFile($this->caminho . '/' . $id);
//			if (is_file($this->caminho . $id)) {
//				
//			}
			$this->view->total = 1;
			$sucesso = true;
		}
		catch (Exception $e) {
			$sucesso = false;
			$this->view->erro = $e;
		}
		
		$this->view->sucesso = $sucesso;
	}
	
}